<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="年级id" prop="gradeId">
      <el-input v-model="dataForm.gradeId" placeholder="年级id"></el-input>
    </el-form-item>
    <el-form-item label="班级类型" prop="clazzType">
      <el-input v-model="dataForm.clazzType" placeholder="班级类型"></el-input>
    </el-form-item>
    <el-form-item label="班级名字" prop="className">
      <el-input v-model="dataForm.className" placeholder="班级名字"></el-input>
    </el-form-item>
    <el-form-item label="代码" prop="code">
      <el-input v-model="dataForm.code" placeholder="代码"></el-input>
    </el-form-item>
    <el-form-item label="别名" prop="alias">
      <el-input v-model="dataForm.alias" placeholder="别名"></el-input>
    </el-form-item>
    <el-form-item label="基因" prop="gene">
      <el-input v-model="dataForm.gene" placeholder="基因"></el-input>
    </el-form-item>
    <el-form-item label="组织ID" prop="schoolId">
      <el-input v-model="dataForm.schoolId" placeholder="组织ID"></el-input>
    </el-form-item>
    <el-form-item label="组织代码" prop="schoolCode">
      <el-input v-model="dataForm.schoolCode" placeholder="组织代码"></el-input>
    </el-form-item>
    <el-form-item label="类型（暂不使用）1：行政；2：教学；3：重点；4：实验；5：特色。" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型（暂不使用）1：行政；2：教学；3：重点；4：实验；5：特色。"></el-input>
    </el-form-item>
    <el-form-item label="学年别名" prop="yearAlias">
      <el-input v-model="dataForm.yearAlias" placeholder="学年别名"></el-input>
    </el-form-item>
    <el-form-item label="学段别名" prop="sectionAlias">
      <el-input v-model="dataForm.sectionAlias" placeholder="学段别名"></el-input>
    </el-form-item>
    <el-form-item label="年级别名" prop="gradeAlias">
      <el-input v-model="dataForm.gradeAlias" placeholder="年级别名"></el-input>
    </el-form-item>
    <el-form-item label="班级编号" prop="number">
      <el-input v-model="dataForm.number" placeholder="班级编号"></el-input>
    </el-form-item>
    <el-form-item label="教室编号" prop="roomId">
      <el-input v-model="dataForm.roomId" placeholder="教室编号"></el-input>
    </el-form-item>
    <el-form-item label="教室代码" prop="roomCode">
      <el-input v-model="dataForm.roomCode" placeholder="教室代码"></el-input>
    </el-form-item>
    <el-form-item label="教室名称" prop="roomName">
      <el-input v-model="dataForm.roomName" placeholder="教室名称"></el-input>
    </el-form-item>
    <el-form-item label="上学期班级人数" prop="termoneTotal">
      <el-input v-model="dataForm.termoneTotal" placeholder="上学期班级人数"></el-input>
    </el-form-item>
    <el-form-item label="下学期班级人数" prop="termtwoTotal">
      <el-input v-model="dataForm.termtwoTotal" placeholder="下学期班级人数"></el-input>
    </el-form-item>
    <el-form-item label="班级简介" prop="brief">
      <el-input v-model="dataForm.brief" placeholder="班级简介"></el-input>
    </el-form-item>
    <el-form-item label="入学年份" prop="joinYear">
      <el-input v-model="dataForm.joinYear" placeholder="入学年份"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="元id(同步saas使用的元id),升班使用" prop="originId">
      <el-input v-model="dataForm.originId" placeholder="元id(同步saas使用的元id),升班使用"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="更新者" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="更新者"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          gradeId: '',
          clazzType: '',
          className: '',
          code: '',
          alias: '',
          gene: '',
          schoolId: '',
          schoolCode: '',
          type: '',
          yearAlias: '',
          sectionAlias: '',
          gradeAlias: '',
          number: '',
          roomId: '',
          roomCode: '',
          roomName: '',
          termoneTotal: '',
          termtwoTotal: '',
          brief: '',
          joinYear: '',
          status: '',
          originId: '',
          createTime: '',
          createBy: '',
          updateTime: '',
          updateBy: ''
        },
        dataRule: {
          gradeId: [
            { required: true, message: '年级id不能为空', trigger: 'blur' }
          ],
          clazzType: [
            { required: true, message: '班级类型不能为空', trigger: 'blur' }
          ],
          className: [
            { required: true, message: '班级名字不能为空', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '代码不能为空', trigger: 'blur' }
          ],
          alias: [
            { required: true, message: '别名不能为空', trigger: 'blur' }
          ],
          gene: [
            { required: true, message: '基因不能为空', trigger: 'blur' }
          ],
          schoolId: [
            { required: true, message: '组织ID不能为空', trigger: 'blur' }
          ],
          schoolCode: [
            { required: true, message: '组织代码不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型（暂不使用）1：行政；2：教学；3：重点；4：实验；5：特色。不能为空', trigger: 'blur' }
          ],
          yearAlias: [
            { required: true, message: '学年别名不能为空', trigger: 'blur' }
          ],
          sectionAlias: [
            { required: true, message: '学段别名不能为空', trigger: 'blur' }
          ],
          gradeAlias: [
            { required: true, message: '年级别名不能为空', trigger: 'blur' }
          ],
          number: [
            { required: true, message: '班级编号不能为空', trigger: 'blur' }
          ],
          roomId: [
            { required: true, message: '教室编号不能为空', trigger: 'blur' }
          ],
          roomCode: [
            { required: true, message: '教室代码不能为空', trigger: 'blur' }
          ],
          roomName: [
            { required: true, message: '教室名称不能为空', trigger: 'blur' }
          ],
          termoneTotal: [
            { required: true, message: '上学期班级人数不能为空', trigger: 'blur' }
          ],
          termtwoTotal: [
            { required: true, message: '下学期班级人数不能为空', trigger: 'blur' }
          ],
          brief: [
            { required: true, message: '班级简介不能为空', trigger: 'blur' }
          ],
          joinYear: [
            { required: true, message: '入学年份不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          originId: [
            { required: true, message: '元id(同步saas使用的元id),升班使用不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/eas/easbasicclazz/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.gradeId = data.easBasicClazz.gradeId
                this.dataForm.clazzType = data.easBasicClazz.clazzType
                this.dataForm.className = data.easBasicClazz.className
                this.dataForm.code = data.easBasicClazz.code
                this.dataForm.alias = data.easBasicClazz.alias
                this.dataForm.gene = data.easBasicClazz.gene
                this.dataForm.schoolId = data.easBasicClazz.schoolId
                this.dataForm.schoolCode = data.easBasicClazz.schoolCode
                this.dataForm.type = data.easBasicClazz.type
                this.dataForm.yearAlias = data.easBasicClazz.yearAlias
                this.dataForm.sectionAlias = data.easBasicClazz.sectionAlias
                this.dataForm.gradeAlias = data.easBasicClazz.gradeAlias
                this.dataForm.number = data.easBasicClazz.number
                this.dataForm.roomId = data.easBasicClazz.roomId
                this.dataForm.roomCode = data.easBasicClazz.roomCode
                this.dataForm.roomName = data.easBasicClazz.roomName
                this.dataForm.termoneTotal = data.easBasicClazz.termoneTotal
                this.dataForm.termtwoTotal = data.easBasicClazz.termtwoTotal
                this.dataForm.brief = data.easBasicClazz.brief
                this.dataForm.joinYear = data.easBasicClazz.joinYear
                this.dataForm.status = data.easBasicClazz.status
                this.dataForm.originId = data.easBasicClazz.originId
                this.dataForm.createTime = data.easBasicClazz.createTime
                this.dataForm.createBy = data.easBasicClazz.createBy
                this.dataForm.updateTime = data.easBasicClazz.updateTime
                this.dataForm.updateBy = data.easBasicClazz.updateBy
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/eas/easbasicclazz/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'gradeId': this.dataForm.gradeId,
                'clazzType': this.dataForm.clazzType,
                'className': this.dataForm.className,
                'code': this.dataForm.code,
                'alias': this.dataForm.alias,
                'gene': this.dataForm.gene,
                'schoolId': this.dataForm.schoolId,
                'schoolCode': this.dataForm.schoolCode,
                'type': this.dataForm.type,
                'yearAlias': this.dataForm.yearAlias,
                'sectionAlias': this.dataForm.sectionAlias,
                'gradeAlias': this.dataForm.gradeAlias,
                'number': this.dataForm.number,
                'roomId': this.dataForm.roomId,
                'roomCode': this.dataForm.roomCode,
                'roomName': this.dataForm.roomName,
                'termoneTotal': this.dataForm.termoneTotal,
                'termtwoTotal': this.dataForm.termtwoTotal,
                'brief': this.dataForm.brief,
                'joinYear': this.dataForm.joinYear,
                'status': this.dataForm.status,
                'originId': this.dataForm.originId,
                'createTime': this.dataForm.createTime,
                'createBy': this.dataForm.createBy,
                'updateTime': this.dataForm.updateTime,
                'updateBy': this.dataForm.updateBy
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
